package org.game.module.event.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.bean.ActivityEntryInfo;
import org.bean.ActivityInfo;
import org.cache.ActivityInfoCache;
import org.constant.ActivityConstant;
import org.constant.smallkey.ActivitySmallKey;
import org.game.db.bean.Player;
import org.game.db.bean.PlayerActivity;
import org.game.db.cache.PlayerActivityCache;
import org.game.manager.ActivityManager;
import org.game.msgBuilder.ActivityMsgBuilder;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventHandler;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventVector;
import org.game.protobuf.s2c.S2CActivityMsg.ActivityInfoMsg;
import org.game.protobuf.s2c.S2CActivityMsg.PushActivitys;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;
/**
 * 活动完成事件处理器
 * @author win7
 *
 */
public class ActivityFinishEventHandler implements GameEventHandler{
	@Override
	public void onEvent(GameEventListener listener, GameEvent event) {
		Player player = event.getPlayer();
		// 取得玩家活动
		List<PlayerActivity> runingPlayerActivitys = PlayerActivityCache.me().getByPlayerId(player.getId());
		for (PlayerActivity activity : runingPlayerActivitys) {
			ActivityInfo activityInfo = ActivityInfoCache.getById(activity.getActivityId());
			if (activityInfo == null) {
				continue;
			}
			
			
			Map<Integer, ActivityEntryInfo> entrys = activityInfo.getEntrys();
			entrys.forEach((entryId,entry)->{
				//完成任务：用的是完成任务的条件参数
				Map params = entry.getFinishParams();
				
				
				Map<Integer, Integer> entryInfo =  (Map<Integer, Integer>) activity.getEntry().computeIfAbsent(entryId, k -> new HashMap<Integer, Integer>(2));
				
				int status = MapUtils.getIntValue(entryInfo, ActivitySmallKey.STATUS);
				
				// 检查待完成的条目
				if (status == ActivityConstant.ENTRY_STATUS_RUNING) {
					handler(listener, event, new ActivityEntryVector(activity,entryInfo), entry.getFinishCondId(), params,entry.getProgress());						
				}
			});
		}
	}


	// 活动条目载体
	public static class ActivityEntryVector implements GameEventVector{
		private Map<Integer, Integer> entry;
		private PlayerActivity activity;
		
		public ActivityEntryVector(PlayerActivity activity,Map<Integer, Integer> entry) {
			super();
			this.entry = entry;
			this.activity = activity;
		}

		@Override
		public int getProgress() {
			return MapUtils.getIntValue(entry, ActivitySmallKey.PROGRESS);
		}

		@Override
		public void setProgress(int progress) {
			entry.put(ActivitySmallKey.PROGRESS, progress);
		}

		@Override
		public void trigger(int maxProcess) {
			if (getProgress() >= maxProcess) {
				entry.put(ActivitySmallKey.STATUS, ActivityConstant.ENTRY_STATUS_FINISH);
			}
			activity.update();
		}

		@Override
		public void notify(Player player) {
			ActivityInfoMsg.Builder activityInfoBuilder = ActivityMsgBuilder.createActivityInfoMsg(player,ChangeType.UPDATE, activity);
			ActivityManager.me().notifyActivity(player,PushActivitys.newBuilder().addActivitys(activityInfoBuilder));
		}
		
	}
}
